#include "sprite.h"

sprite_data::sprite_data(std::string path)
{
    texture = game::instance().getAssetStore()->getImage(path);
    SDL_GetTextureSize(texture, &src.w, &src.h);
}

void sprite::render()
{
    if(!data_.texture|| !parent_| isFinished_)
        return;
    auto pos = parent_->getRenderPosition() + offset_;
    game_.renderSprite(data_, pos, size_ ,preScale_);
}

void sprite::setData(const sprite_data &data)
{
    data_ = data;
    size_ = {data.src.w, data.src.h};
}

sprite *sprite::createSprite(screen *parent, const char *path, float scale,Anchor anchro)
{
    auto sprite_ = new sprite();
    sprite_->init();
    sprite_->setAnchor(anchro);
    sprite_->setData(sprite_data(path));
    sprite_->setScale(scale);
    sprite_->setParent(parent);
    if(parent)
        parent->addChild(sprite_);
    return sprite_;
}
